// 修改 useQuestionBankDataStore
import { defineStore } from 'pinia'
import { questionBanListType, ChangeQuestion } from '@/types/QuestionBankType'
import { QuestionBankApi } from "@/api/QuestionBank/QuestionBank.ts";

/**
 * 题库数据存储
 */
export const useQuestionBankDataStore = defineStore('questionBankData', {
    state: () => ({
        questionBankData: null as questionBanListType | null, // 题库
    }),
    actions: {
        // 更新题库数据并存储到 localStorage
        setQuestionBankData(questionData: questionBanListType) {
            this.questionBankData = questionData;
            // 将数据存储到 localStorage
            localStorage.setItem('questionBankData', JSON.stringify(questionData));
        },
        // 清除题库数据并删除 localStorage
        clearQuestionBankData() {
            this.questionBankData = null;
            localStorage.removeItem('questionBankData');
        },
        // 从 localStorage 恢复数据
        restoreData() {
            const data = localStorage.getItem('questionBankData');
            this.questionBankData = JSON.parse(data);
            // console.log('取出来的数据'+data);
        },
    },
});

// 在应用启动时调用 restoreData 恢复数据
const questionStore = useQuestionBankDataStore();
questionStore.restoreData();


/**
 * 题库题目
 */
export const useQuestionBanTopicStore = defineStore('topic', {
    state: () => ({
        ChangeQuestion: null as ChangeQuestion | null,        // 当前题目
    }),
    actions: {
        handChangeQuestion(ChangeQuestion){
            useQuestionBankDataStore().questionBankData= ChangeQuestion;
            console.log(useQuestionBankDataStore().questionBankData);
            useQuestionBankDataStore().setQuestionBankData;
        
        }
        // 从 API 获取题库列表并存储到 state 中
    //     async fetchQuestionBankById(params: ChangeQuestion) {
    //         try {
    //             const data = await QuestionBankApi.getQuestionBankListById(params);
    //             console.log(data);
    //             this.questionBankId = data.data || [];
    //         } catch (error) {
    //             console.error('获取题库列表失败:', error);
    //             throw error; // 可以选择抛出错误或处理错误
    //         }
    //     },
    }
});



// export const useQuestionBanTopicStore = defineStore('topic', {
//     state: () => ({
//         ChangeQuestion: null as ChangeQuestion | null,
//         questionBankList: [] as ChangeQuestion[],
//         loading: false,
//         error: null as string | null
//     }),
//     actions: {
//         // 从 API 获取题目并存储
//         async fetchQuestionBankById(params: { exercisesId: number }) {
//             this.loading = true;
//             this.error = null;
//             try {
//                 const response = await QuestionBankApi.getQuestionBankListById(params);
                
//                 // 更新状态
//                 this.ChangeQuestion = response.data;
//                 // 如果需要存储列表
//                 this.questionBankList = response.data || []; 
                
//                 // 存入本地存储
//                 localStorage.setItem('currentQuestion', JSON.stringify(response.data));
                
//                 return response;
//             } catch (error) {
//                 this.error = '获取题目失败';
//                 console.error('获取题库列表失败:', error);
//                 throw error;
//             } finally {
//                 this.loading = false;
//             }
//         },
        
//         // 从本地存储恢复数据
//         restoreData() {
//             const stored = localStorage.getItem('currentQuestion');
//             if (stored) {
//                 this.ChangeQuestion = JSON.parse(stored);
//             }
//         },
        
//         // 清除数据
//         clearData() {
//             this.ChangeQuestion = null;
//             this.questionBankList = [];
//             localStorage.removeItem('currentQuestion');
//         }
//     },
//     getters: {
//         currentQuestion: (state) => state.ChangeQuestion,
//         isLoading: (state) => state.loading
//     }
// });
